#!/usr/bin/env python
# coding=utf-8
# Created on 2015年6月25日
# author: ep@zmeng
import sys
import logging
import logging.config;

_glogging_instance = 0

def _gen_conf(base = "/var/log/", level = 'all'):
    logging_dir = base;
    logging_conf = {
        'version': 1,
        'formatters': {
            'simpleFormatter': {
                'format': '%(message)s'
                },
            'displayFormatter': {
                'format': '[%(asctime)s] %(message)s'
                },
            'fileFormatter': {
                'format': '[%(asctime)s] %(message)s'
                }
            },
        'handlers': {
            'rootHandler': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'fileFormatter',
                'maxBytes': 20 * 1024 * 1024,
                'backupCount': 7,
                'filename': logging_dir + '/sys-info.log',
                'mode': 'a'
                },
            'consoleHandler': {
                'class': 'logging.StreamHandler',
                'level': 'DEBUG',
                'formatter': 'displayFormatter',
                'stream': sys.stdout
                },
            'RF-State-Error-Handler': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'ERROR',
                'formatter': 'fileFormatter',
                'backupCount': 7,
                'filename': logging_dir + '/state-error.log',
                'mode': 'a'
                },
            'RF-State-Info-Handler': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'fileFormatter',
                'backupCount': 7,
                'filename': logging_dir + '/state-info.log',
                'mode': 'a'
                },
            'RF-Process-Error-Handler': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'ERROR',
                'formatter': 'fileFormatter',
                'backupCount': 7,
                'filename': logging_dir + '/process-error.log',
                'mode': 'a'
                },
            'RF-Process-Info-Handler': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'fileFormatter',
                'backupCount': 7,
                'filename': logging_dir + '/process-info.log',
                'mode':  'a'
                }
            },
        'loggers': {
                'root': {
                    'level': 'DEBUG',
                    'handlers': ['rootHandler']
                    },
                'state': {
                    'level': 'DEBUG',
                    'handlers': ['RF-State-Error-Handler', 'RF-State-Info-Handler'],
                    'qualname': 'state',
                    'propagate': 0
                    },
                'process': {
                    'level': 'INFO',
                    'handlers': ['RF-Process-Info-Handler', 'RF-Process-Error-Handler'],
                    'qualname': 'process',
                    'propagate': 0
                    }
                }
        }

    return logging_conf;

def glogging_state_info(msg = None):
    if _glogging_instance and msg:
        logging.getLogger('state').info(msg);

def glogging_state_error(msg = None):
    if _glogging_instance and msg:
        logging.getLogger('state').error(msg);

def glogging_process_info(msg = None):
    if _glogging_instance and msg:
        logging.getLogger('process').info(msg);

def glogging_process_error(msg = None):
    if _glogging_instance and msg:
        logging.getLogger('process').error(msg);

def glogging_init(logdir = '/var/log/', level = 'all'):
    global _glogging_instance
    _glogging_instance = 1
    logging.config.dictConfig(_gen_conf(logdir, level));


